/**--------------------------------------------*/
/** JS:  cmi_role_edit.js（角色管理--编辑）          */
/**--------------------------------------------*/
var win = null;
var tipc = 'ADMIN_POWER_CMI_ROLE';
var pref = 'SYS_';
var isAdd = true;

var checkMenu = function(id){
	var checked = document.getElementById(id).checked;
	$('input[id*="'+id+'"]').attr("checked",checked);
};

var checkMenuItem = function(id,father) {
	var checked = document.getElementById(id).checked;
	$('#'+id).attr("checked",checked);
	
	if(checked) {
		$('#'+father).attr("checked",checked);
		$('input[id*="'+id+'"]').attr("checked",checked);
	} else {
		$('input[id*="'+id+'"]').attr("checked",checked);
		//清除父亲的选中，当父亲没有任何被选中的子项时
//		checkMenu(id);
//		var t = $('input[id*="'+father+'"]:checked');
//		if(t.length == 1) {
//			$('#'+father).attr("checked",false);
//		}
		
	}
};

var chechFunction = function(id,father) {
	var checked = document.getElementById(id).checked;
	$('#'+id).attr("checked",checked);
	if(checked) {
		$('#'+father).attr("checked",checked);
		$('#'+$('#'+father).attr("father")).attr("checked",checked);
	} else {
		//清除父亲的选中，当父亲没有任何被选中的子项时
//		var t = $('input[id*="'+father+'"]:checked');
//		if(t.length == 1) {
//			$('#'+father).attr("checked",false);
//			checkMenuItem(father,$('#'+father).attr("father"));
//		}
	}
};

var createMenu = function(prefix,record) {
	var id =prefix + record.id + "_";
	var menu = '<td class="first-cell" valign="top" align="left">'
			+'<input type="checkbox" onclick="checkMenu(\''+id+'\');" father="0" id="'+id+'" value="'+record.id+'">'
			+record.text
			+'</td><td align="left" valign="top" style="border-left: 1px solid gray;">';
	
	var children = record.children;
	if(children.length > 0) {
		for(var j = 0; j < children.length; j++){
			menu +=createMenuItem(id,children[j]);
		}
	}
	return menu + '</td>';
};

var createMenuItem = function(prefix,record) {
	var id =prefix + record.id + "_";
	var menuItem = '<div style="float:left;">'
			+'<table cellspacing="1"><tbody>'
			+'<tr>'
			+'<td class="first-cell" valign="top" align="left">'
			+'<input id="'+id+'" type="checkbox" onclick="checkMenuItem(\''+id+'\',\''+prefix+'\');" father="'+prefix+'" title="" value="'+record.id+'" >'
			+record.text
			+'</td>';
			
	var children = record.children;
	if(children.length > 0) {
		menuItem += '<td align="left" style="border-left: 1px solid gray;">';
		for(var j = 0; j < children.length; j++){
			menuItem +=createFunction(id,children[j]);
		}
		menuItem +='</td>';
	}
	menuItem += '</tr><tbody></table></div>';
	return menuItem;
};

var createFunction = function(prefix,record) {
	var id =prefix + record.id + "_";
	var functions = '<div style="width:200px;float:left;">'
			+'<label>'
			+'<input id="'+id+'" type="checkbox" onclick="chechFunction(\''+id+'\',\''+prefix+'\');" title="" value="'+record.id+'" >'
			+record.text
			+'</label>'
			+'</div>';
	return functions;
};

var clearData = function() {
	$('#roleName').val('');
	$('#roleDesc').val('');
	$('#uuid').val('');
	$('input[checked="checked"]').attr("checked",false);
};

function submit(url,data) {
	MyUtils.openMask(_msg_process,$('#t2'));
	
	MyUtils.sendPost(url,data,function(msg){
		try{
			MyUtils.hiddenMask($('#t2'));
			var res = MyUtils.toJsonObject(msg);
			if(res.error == '0') {
				$.messager.confirm(_msg_sys,MyUtils.valueAlign(_msg_success + "是否转入[角色列表]","left"), function(r) {
					if(r){
						$('#_back_role').click();
					} else {
						if(isAdd) {
							clearData();
						}
					}
				});
			} else {
				MyUtils.error(res.msg);
			}
		} catch (e) {
			MyUtils.hiddenMask($('#t2'));
			MyUtils.error(_msg_fail);
		}
	});
}

$(function(){
	try {
		//返回角色列表
		$('#_back_role').linkbutton({
			plain : true
		}).click(function(){
			var url = '../dispatch.adm?url=/dispatch.adm&p=web/pages/back/cmi_power/cmi_role_list&menuId='+error.menuId;
			location.replace(url);
		});
		
		//保存提交
		$('#_save_role').linkbutton({
			plain : true
		}).click(function(){
			try{
				//获取数据
				var uuid= $('#uuid').val();
				var roleName = $('#roleName').val();
				var roleDesc = $('#roleDesc').val();
				
				if(MyUtils.isEmpty(roleName)||MyUtils.isEmpty(roleDesc)) {
					MyUtils.waring("带红色*号项的数据不能为空!");
					return;
				}
				
				var checks = $('input[checked="checked"]');
				var ids = new Array();
				for(var i =0 ;i < checks.length; i++) {
					ids.push(checks[i].value);
				}
				var dt = {uuid:uuid,roleName:roleName,roleDesc:roleDesc,rolePower:(ids.length ==0) ? "0":ids.toString()};
				var url = isAdd ? "../impl_add/backAdd.adm":"../impl_modify/backModify.adm";
				var data = {dt:MyUtils.toJsonStr(dt),st:tipc,qy:"[]"};
				
				if(isAdd) {
					submit(url,data);
				} else {
					$.messager.confirm(_msg_sys,MyUtils.valueAlign(_modify_confirm,"left"), function(r) {
						if(r) {
							submit(url,data);
						}
					});
				}
			} catch (e) {
				MyUtils.hiddenMask($('#t2'));
				showJsException(e);
			}
		});
		
		//加载成功
		if(error.code == 0) {
			if(!MyUtils.isEmpty(error.dt)) {
				var role = error.dt;
				$('#uuid').val(role.uuid);
				$('#roleName').val(role.roleName);
				$('#roleDesc').val(role.roleDesc);
				isAdd = false;
			} else {
				isAdd = true;
			}
			
			if(!MyUtils.isEmpty(error.business)){
				$('#empty_power').hide();
				$('#list-table tbody').empty();
				$('#list-table').show();
				
				//屏蔽系统
				var children = error.business[0].children;
				var system = "";
				for(var i = 0; i < children.length; i++) {
					system +='<tr>' + createMenu(pref,children[i]) + "</tr>";
				}
				$('#list-table tbody').append(system);
			} else {
				$('#list-table').hide();
				$('#list-table tbody').empty();
				$('#empty_power').show();
			}
			if(!MyUtils.isEmpty(error.power)){
				var ids = new Array();
				for(var i = 0; i < error.power.length; i++) {
					var menu = error.power[i];
					ids.push("#"+pref + menu.id + "_");
					
					for(var j = 0; j < menu.children.length; j++){
						var menuItem = menu.children[j];
						ids.push("#"+pref + menu.id + "_" + menuItem.id + "_");
						
						for(var v = 0; v < menuItem.children.length; v++){
							var func = menuItem.children[v];
							ids.push("#"+pref + menu.id + "_" + menuItem.id + "_" + func.id + "_");
						}
					}
				}
				if(ids.length > 0) {
					$(ids.toString()).attr("checked",true);
				}
			}
		} else {
			MyUtils.error("页面信息加载失败!");
		}
	} catch (e) {
		showJsException(e);
	}
});
